class ThemeInit {
  static selected = {}
  // static bgmAudio = document.createElement('audio')
  // static gameAudio = document.createElement('audio')
  static handleSetting() {
    document.getElementById('setting').style.visibility = 'visible'
    if (ThemeInit.selected.init) {
      return
    }
    if (localStorage.gameTheme) {
      ThemeInit.selected = JSON.parse(localStorage.gameTheme)
    }
    ThemeInit.selected.init = true
    let theme, music
    //样式列表
    let req = new XMLHttpRequest()
    req.open('get', './theme.json')
    req.send(null)
    req.onload = function () {
      if (req.status == 200) {
        theme = JSON.parse(req.responseText)
        if (theme) {
          //颜色样式列表
          if (theme.colors) {
            theme.colors.forEach(item => {
              let box = document.createElement('div')
              box.style.display = 'flex'
              box.style.alignItems = 'center'
              box.id = 's_color_' + item.id
              box.addEventListener('click', function () {
                for (let item of document.getElementById('s_colors').children) {
                  item.classList.remove('select_color')
                }
                box.classList.add('select_color')
                ThemeInit.selected.color = item
              })
              let div = document.createElement('div')
              div.id = 'color_' + item.id
              div.style.background = item.show
              div.style.border = 'dimgray solid 2px'
              div.style.width = '24px'
              div.style.height = '24px'
              div.style.borderRadius = '10px'
              div.style.margin = '4px'
              box.appendChild(div)
              let txt = document.createTextNode(item.name)
              box.appendChild(txt)
              document.getElementById('s_colors').appendChild(box)
            })
          }
          //字体列表
          if (theme.fonts) {
            theme.fonts.forEach(item => {
              let option = document.createElement('option')
              option.value = 's_font_' + item.id
              option.text = item.name
              option.addEventListener('click', function () {
                ThemeInit.selected.font = item
              })
              document.getElementById('s_font').appendChild(option)
            })
          }
        }
        if (ThemeInit.selected.color) document.getElementById('s_color_' + ThemeInit.selected.color.id).classList.add('select_color')
        if (ThemeInit.selected.font) document.getElementById('s_font').value = 's_font_' + ThemeInit.selected.font.id
      }
    }
    //音乐列表
    // let req1 = new XMLHttpRequest()
    // req1.open('get', './music/music.json')
    // req1.send(null)
    // req1.onload = function () {
    //   if (req1.status == 200) {
    //     music = JSON.parse(req1.responseText)
    //     //背景音
    //     if (music.bgm) {
    //       music.bgm.forEach(item => {
    //         let option = document.createElement('option')
    //         option.value = 's_music_bgm_' + item.id
    //         option.text = item.name
    //         option.addEventListener('click', function () {
    //           ThemeInit.selected.bgm = item
    //           ThemeInit.bgmAudio.src = '..' + item.url
    //           ThemeInit.bgmAudio.loop = true
    //           ThemeInit.bgmAudio.play()
    //         })
    //         document.getElementById('s_music_bgm').appendChild(option)
    //       })
    //     }
    //     //游戏音
    //     if (music.game) {
    //       music.game.forEach(item => {
    //         let option = document.createElement('option')
    //         option.value = 's_music_game_' + item.id
    //         option.text = item.name
    //         option.addEventListener('click', function () {
    //           ThemeInit.selected.game_music = item
    //           ThemeInit.gameAudio.src = '..' + item.introduce
    //           ThemeInit.gameAudio.play()
    //         })
    //         document.getElementById('s_music_game').appendChild(option)
    //       })
    //     }
    //     if (ThemeInit.selected.bgm) document.getElementById('s_music_bgm').value = 's_music_bgm_' + ThemeInit.selected.bgm.id
    //     if (ThemeInit.selected.game_music) {
    //       document.getElementById('s_music_game').value = 's_music_game_' + ThemeInit.selected.game_music.id
    //     }
    //   }
    // }
  }
  static saveTheme() {
    localStorage.gameTheme = JSON.stringify(ThemeInit.selected)
    location.reload()
  }
  static settingBack() {
    document.getElementById('setting').style.visibility = 'hidden'
    // ThemeInit.bgmAudio.pause()
  }
}
